package com.arch.dev.client;

import java.util.List;
import java.util.Set;

import com.arch.dev.client.view.ui.BudgetToolBar;
import com.arch.dev.client.vo.BudgetVO;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
import com.google.gwt.view.client.SelectionModel;

public class BudgetTable extends Composite {

	private static BudgetTableUiBinder uiBinder = GWT

	.create(BudgetTableUiBinder.class);

	interface BudgetTableUiBinder extends UiBinder<Widget, BudgetTable> {

	}

	@UiField(provided = true)
	CellTable<BudgetVO> cellTable;

	@UiField(provided = true)
	SimplePager pager;

	@UiField(provided = true)
	BudgetToolBar budgetToolBar;

	Widget view;

	ListDataProvider<BudgetVO> dataProvider;

	public List<BudgetVO> getBudgetVOs() {
		return budgetVOs;
	}

	public BudgetTable() {

		onInitialize();

		initWidget(uiBinder.createAndBindUi(this));

	}

	public BudgetTable(Widget view) {

		this();

		this.view = view;

	}

	SelectionModel<BudgetVO> selectionModel;

	public void onInitialize() {
		
		

		budgetToolBar = new BudgetToolBar();
		budgetToolBar.removeAllWidgets();
		//deleteImageButton  = new ImageButton(null, bundle.getDeleteIcon());
		//budgetToolBar.addToolWidget(deleteImageButton);
		/*budgetToolBar.addClickHandler(2, new ClickHandler() {

			public void onClick(ClickEvent event) {

				MultiSelectionModel multiSelectionModel = (MultiSelectionModel<BudgetVO>) selectionModel;

				Set<BudgetVO> budgetVOs = multiSelectionModel.getSelectedSet();

				multiSelectionModel.clear();

				BudgetController.getInstance().deleteValues(view, budgetVOs);

			}

		});*/

		//budgetToolBar.setToolTip(2, "Delete Records");
		//budgetToolBar.setVisible(0, false);
		//budgetToolBar.setVisible(1, false);

		cellTable = new CellTable<BudgetVO>();

		cellTable.setWidth("100%", true);

		// Add a selection model so we can select cells.

		selectionModel = new MultiSelectionModel<BudgetVO>(
				BudgetVO.KEY_PROVIDER);

		cellTable
				.setSelectionModel(selectionModel, DefaultSelectionEventManager
						.<BudgetVO> createCheckboxManager());

		// Create a Pager to control the table.

		SimplePager.Resources pagerResources = GWT

		.create(SimplePager.Resources.class);

		pager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0,

		true);

		pager.setDisplay(cellTable);

		// Initialize the columns.

		initTableColumns();

		dataProvider = new ListDataProvider<BudgetVO>();

		// Connect the table to the data provider.

		dataProvider.addDataDisplay(cellTable);

	}

	/**
	 * 
	 * Add the columns to the table.
	 */

	private void initTableColumns() {

		// Create name column.

		TextColumn<BudgetVO> nameColumn = new TextColumn<BudgetVO>() {

			@Override
			public String getValue(BudgetVO contact) {

				return contact.getTitle();

			}

		};

		// Make the name column sortable.

		nameColumn.setSortable(true);

		final DateTimeFormat dateFormat = DateTimeFormat

		.getFormat(PredefinedFormat.DATE_FULL);

		// Create address column.

		TextColumn<BudgetVO> addressColumn = new TextColumn<BudgetVO>() {

			@Override
			public String getValue(BudgetVO contact) {

				return dateFormat.format(contact.getDate());

			}

		};

		// Create name column.

		TextColumn<BudgetVO> catagoryColumn = new TextColumn<BudgetVO>() {

			@Override
			public String getValue(BudgetVO contact) {

				return contact.getCatagoryVO().getName();

			}

		};

		// Create name column.

		TextColumn<BudgetVO> costColumn = new TextColumn<BudgetVO>() {

			@Override
			public String getValue(BudgetVO contact) {

				return contact.getCost().toString();

			}

		};

		// Add the columns.

		// table.addColumn(rowCountColumn, "#");

		Column<BudgetVO, Boolean> checkColumn = new Column<BudgetVO, Boolean>(
				new CheckboxCell(true, false)) {

			@Override
			public Boolean getValue(BudgetVO object) {

				return selectionModel.isSelected(object);

			}

		};

		cellTable.setColumnWidth(checkColumn, 100, Unit.PX);
		// cellTable.setColumnWidth(catagoryColumn, 20, Unit.PC);
		// cellTable.setColumnWidth(addressColumn, 20, Unit.PC);
		cellTable.setColumnWidth(costColumn, 150, Unit.PX);
		cellTable.addColumn(checkColumn,
				SafeHtmlUtils.fromSafeConstant("<br/>"));

		cellTable.addColumn(nameColumn, "Description");

		cellTable.addColumn(catagoryColumn, "Catagory");

		cellTable.addColumn(costColumn, "Cost");

		cellTable.addColumn(addressColumn, "Date");

	

	}

	List<BudgetVO> budgetVOs;
	
	public void populateflexTable(List<BudgetVO> budgetVOs) {
		this.budgetVOs = budgetVOs;
		
		List<BudgetVO> list = dataProvider.getList();

		list.clear();

		for (BudgetVO budgetVO : budgetVOs) {

			list.add(budgetVO);

		}

		cellTable.setPageSize(10);
		
	}

	class BudgetSelectionChangeHandler implements Handler {

		public void onSelectionChange(SelectionChangeEvent event) {

			MultiSelectionModel<BudgetVO> selectionModel = (MultiSelectionModel<BudgetVO>) event
					.getSource();

			Set<BudgetVO> selected = selectionModel.getSelectedSet();

		}

	}

	public MultiSelectionModel<BudgetVO> getMultiSelectionModel() {
		return (MultiSelectionModel<BudgetVO>) selectionModel;
	}
	
	public void addToolBarWidget(Widget widget){
		budgetToolBar.addToolWidget(widget);
	}

	/*public void addDeleteButtonClickHandler(ClickHandler handler) {
		//deleteButton.addClickHandler(handler);
		deleteImageButton.addClickHandler(handler);
	}
*/
}